import ChatDB from '@/components/systemWindow/weChat/chatDB'
import { defineStore } from 'pinia'
import { dateFormat, randomWord } from '@/utils/utils'
import type { IChat, IChatDB } from '@/components/systemWindow/weChat/chatDB'

export interface IWeChatDB {
  chatDB: IChatDB[]
}

export const useWeChatDBStore = defineStore('WeChatDB', {
  state: (): IWeChatDB => {
    return {
      chatDB: ChatDB.chatDB
    }
  },
  actions: {
    sendChat(id: string, content: string, type: IChat['type'] = 'sender') {
      const find = this.chatDB.find((item: IChatDB) => item.id === id)
      if (find) {
        find.list.push({
          id: randomWord(20, 'chat-'),
          type,
          content: content,
          user: 'Fivexu',
          date: dateFormat('YYYY-MM-DD hh:mm:ss'),
          avatar: 'avatar'
        })
      }
    }
  },
  persist: true
})
